package br.indt.framework.ui.common;

import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

import br.indt.framework.log.MicrologWrapper;

/**
 * @author Indt
 * This class is responsible to create an option menu.
 */
public abstract class OptionMenuScreenBase implements MenuItemListener, CommandListener {
	
	private static final MicrologWrapper log = MicrologWrapper.getLogger(OptionMenuScreenBase.class);
	
	protected Form form  = null;
	protected MenuItem rskItem = null;
	protected Command backCmd = null;
	
	protected int menubar_size = 0;
	
	public static int gethItem() {
		
		switch (Display.getDeviceType()) {
		case Display.TYPE:
			return 46;

		case Display.TOUCH_TYPE:
			return 39;

		case Display.FULL_TOUCH:
			return 46;

		case Display.TYPE_LANDSCAPE:
			return 35;
		case Display.NEXT_GENERATION:
			return 46;
		}
		return 0;
	
	}

	public OptionMenuScreenBase(Vector optionList) {
		form = new Form("OptionMenu");
		menubar_size = Display.isTouchAndType()?Display.MENU_BAR_HEIGHT_TOUCH : Display.MENU_BAR_HEIGHT;
		if (Display.getDeviceType() == Display.NEXT_GENERATION){
			menubar_size = 10;
			backCmd = new Command("Back", Command.BACK, 0);
			form.addCommand(backCmd);
			form.setCommandListener(this);
		}

	}

	public void show() {
		this.form.menuListShow = true;
		try {
			
			Display.getInstance().push(form);

		} catch (Exception e) {
			//#ifdef LOGS
			log.error("OptionMenuScreen:: show : " + e.getMessage());
			//#endif
		}
	}

	public void populate(Vector options) {
		int count = options.size();

		for (int i = 0; i < count; i++) {
			Object opt =  options.elementAt(i);
			OptionMenuItem ci = (OptionMenuItem) opt;

			form.add(ci, false);
		}
	}

	public void menuItemClicked(MenuItem menuItem) {
		if (menuItem == rskItem) {
			Display.getInstance().setControllerListener(true);
			Display.getInstance().pop();
		}
	}

	public MenuItem getRskItem() {
		return rskItem;
	}
	
	public void commandAction(Command cmd, Displayable d) {
		if(cmd == backCmd){
			Display.getInstance().setControllerListener(true);
			Display.getInstance().pop();
		}
	}
	
}